home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 August / CHIP_CD_2004-08.iso / software / amc / amc_install.exe / {app} / Scripts / IMDB.ifs < prev    next >
Encoding:
Text File  |  2004-03-21  |  11.3 KB  |  398 lines

  1. // GETINFO SCRIPTING
  2. // IMDB (US) import
  3.  
  4. (***************************************************
  5.  *  Movie importation script for:                  *
  6.  *      IMDB (US), http://us.imdb.com              *
  7.  *                                                 *
  8.  *  (c) 2002-2004 Antoine Potten                   *
  9.  *                          software@antp.be       *
  10.  *  Contributors :                                 *
  11.  *    Danny Falkov                                 *
  12.  *    Kai Blankenhorn                              *
  13.  *                                                 *
  14.  *  For use with Ant Movie Catalog 3.4.0           *
  15.  *  www.antp.be/software/moviecatalog              *
  16.  *                                                 *
  17.  *  This program is free software; you can         *
  18.  *  redistribute it and/or modify it under the     *
  19.  *  terms of the GNU General Public License as     *
  20.  *  published by the Free Software Foundation;     *
  21.  *  either version 2 of the License, or (at your   *
  22.  *  option) any later version.                     *
  23.  ***************************************************)
  24.  
  25. program IMDb;
  26.  
  27. const
  28.   DescriptionToImport = 2;
  29.    {
  30.       2 = import longest
  31.       1 = import short (from main page, faster)
  32.       0 = display list to select a description
  33.    }
  34.  
  35. var
  36.   MovieName: string;
  37.   MovieURL: string;
  38.  
  39. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  40. var
  41.   i: Integer;
  42. begin
  43.   result := -1;
  44.   if StartAt < 0 then
  45.     StartAt := 0;
  46.   for i := StartAt to List.Count-1 do
  47.     if Pos(Pattern, List.GetString(i)) <> 0 then
  48.     begin
  49.       result := i;
  50.       Break;
  51.     end;
  52. end;
  53.  
  54. procedure AnalyzePage(Address: string);
  55. var
  56.   Page: TStringList;
  57. begin
  58.   Page := TStringList.Create;
  59.   Page.Text := GetPage(Address);
  60.   if pos('<title>IMDb', Page.Text) = 0 then
  61.   begin
  62.     AnalyzeMoviePage(Page)
  63.   end else
  64.   begin
  65.     PickTreeClear;
  66.     AddMoviesTitles(Page, '<b>Exact Matches</b>');
  67.     AddMoviesTitles(Page, '<b>Partial Matches</b>');
  68.     AddMoviesTitles(Page, '<b>Approximate Matches</b>');
  69.     if PickTreeExec(Address) then
  70.       AnalyzePage(Address);
  71.   end;
  72.   Page.Free;
  73. end;
  74.  
  75. procedure AnalyzeMoviePage(Page: TStringList);
  76. var
  77.   Line, Value, Value2, FullValue: string;
  78.   LineNr: Integer;
  79.   BeginPos, EndPos, DescrImport: Integer;
  80. begin
  81.   DescrImport := DescriptionToImport;
  82.   if (DescrImport <> 1) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
  83.     DescrImport := 1;
  84.  
  85.   MovieURL := 'http://imdb.com/title/tt' + copy(Page.Text, pos('<a href="/title/tt',Page.Text)+19, 7);
  86.  
  87.   // URL
  88.   SetField(fieldURL, MovieURL);
  89.  
  90.   // Original Title & Year
  91.   LineNr := FindLine('<title>', Page, 0);
  92.   Line := Page.GetString(LineNr);
  93.   if LineNr > -1 then
  94.   begin
  95.     BeginPos := pos('<title>', Line);
  96.     if BeginPos > 0 then
  97.       BeginPos := BeginPos + 7;
  98.     EndPos := pos('(', Line);
  99.     if EndPos = 0 then
  100.       EndPos := Length(Line);
  101.     Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
  102.     HTMLDecode(Value);
  103.     SetField(fieldOriginalTitle, Value);
  104.     BeginPos := pos('(', Line) + 1;
  105.     if BeginPos > 0 then
  106.     begin
  107.       EndPos := Pos('/I', Line);
  108.       if EndPos < BeginPos then
  109.         EndPos := Pos(')', Line);
  110.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  111.       SetField(fieldYear, Value);
  112.     end;
  113.   end;
  114.  
  115.   // Rating
  116.   LineNr := FindLine('User Rating:', Page, 0);
  117.   if LineNr > -1 then
  118.   begin
  119.     Line := Page.GetString(LineNr + 4);
  120.     if Pos('/10', Line) > 0 then
  121.     begin
  122.       BeginPos := pos('<b>', Line) + 3;
  123.       Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
  124.       SetField(fieldRating, Value);
  125.     end;
  126.   end;
  127.  
  128.   // Director
  129.   LineNr := FindLine('Directed by', Page, 0);
  130.   if LineNr > -1 then
  131.   begin
  132.     FullValue := '';
  133.     Line := Page.GetString(LineNr + 1);
  134.     repeat
  135.       BeginPos := pos('">', Line) + 2;
  136.       EndPos := pos('</a>', Line);
  137.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  138.       if (Value <> '(more)') and (Value <> '') then
  139.       begin
  140.         if FullValue <> '' then
  141.           FullValue := FullValue + ', ';
  142.         FullValue := FullValue + Value;
  143.       end;
  144.       Delete(Line, 1, EndPos);
  145.     until Pos('</a>', Line) = 0;
  146.     HTMLDecode(FullValue);
  147.     SetField(fieldDirector, FullValue);
  148.   end;
  149.  
  150.   // Actors
  151.   LineNr := FindLine('ast overview', Page, 0);
  152.   if LineNr = -1 then
  153.     LineNr := FindLine('redited cast', Page, 0);
  154.   if LineNr > -1 then
  155.   begin
  156.     FullValue := '';
  157.     Line := Page.GetString(LineNr);
  158.     repeat
  159.       BeginPos := Pos('<td valign="top">', Line);
  160.       if BeginPos > 0 then
  161.       begin
  162.         Delete(Line, 1, BeginPos);
  163.         Line := copy(Line, 25, Length(Line));
  164.         BeginPos := pos('">', Line) + 2;
  165.         EndPos := pos('</a>', Line);
  166.         if EndPos = 0 then
  167.           EndPos := Pos('</td>', Line);
  168.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  169.         if (Value <> '(more)') and (Value <> '') then
  170.         begin
  171.           BeginPos := pos('.... </td><td valign="top">', Line);
  172.           if BeginPos > 0 then
  173.           begin
  174.             EndPos := pos('</td></tr>', Line);
  175.             BeginPos := BeginPos + 27;
  176.             Value2 := copy(Line, BeginPos, EndPos - BeginPos);
  177.             if Value2 <> '' then
  178.             begin
  179.               Value := Value + ' (as ' + Value2 + ')';
  180.             end;
  181.           end;
  182.           if FullValue <> '' then
  183.             FullValue := FullValue + ', ';
  184.           FullValue := FullValue + Value;
  185.         end;
  186.         EndPos := Pos('</td></tr>', Line);
  187.         Delete(Line, 1, EndPos);
  188.       end else
  189.       begin
  190.         Line := '';
  191.       end;
  192.     until Line = '';
  193.     HTMLDecode(FullValue);
  194.     SetField(fieldActors, FullValue);
  195.   end;
  196.  
  197.   //Country
  198.   LineNr := FindLine('Country:', Page, 0);
  199.   if LineNr > -1 then
  200.   begin
  201.     Line := Page.GetString(LineNr + 1);
  202.     BeginPos := pos('/">', Line) + 3;
  203.     EndPos := pos('</a>', Line);
  204.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  205.     HTMLDecode(Value);
  206.     SetField(fieldCountry, Value);
  207.   end;
  208.  
  209.   //Category
  210.   LineNr := FindLine('Genre:', Page, 0);
  211.   if LineNr > -1 then
  212.   begin
  213.     Line := Page.GetString(LineNr + 1);
  214.     BeginPos := pos('/">', Line) + 3;
  215.     EndPos := pos('</a>', Line);
  216.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  217.     HTMLDecode(Value);
  218.     SetField(fieldCategory, Value);
  219.   end;
  220.  
  221.   //Description
  222.   LineNr := FindLine('Plot Summary:', Page, 0);
  223.   if LineNr < 1 then
  224.     LineNr := FindLine('Plot Outline:', Page, 0);
  225.   if LineNr > -1 then
  226.   begin
  227.     Line := Page.GetString(LineNr);
  228.     BeginPos := pos('</b>', Line) + 5;
  229.     EndPos := pos('<a href', Line);
  230.     if EndPos < 1 then
  231.     begin
  232.       Line := Line + Page.GetString(LineNr+1);
  233.       EndPos := pos('<br><br>', Line);
  234.       if EndPos < 1 then
  235.         EndPos := Length(Line);
  236.     end;
  237.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  238.     HTMLDecode(Value);
  239.     case DescrImport of
  240.       0:
  241.         begin
  242.           PickListClear;
  243.           PickListAdd(Value);
  244.           GetDescriptions(GetField(fieldURL) + 'plotsummary');
  245.           if PickListExec('Select a description for "' + MovieName + '"', Value) then
  246.             SetField(fieldDescription, Value);
  247.         end;
  248.       1:
  249.         SetField(fieldDescription, Value);
  250.       2:
  251.         SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'));
  252.     end;
  253.   end;
  254.  
  255.   // Comments
  256.   LineNr := FindLine('<b>Summary:</b>', Page, 0);
  257.   if LineNr > -1 then
  258.   begin
  259.     Value := '';
  260.     repeat
  261.       LineNr := LineNr + 1;
  262.       Line := Page.GetString(LineNr);
  263.       EndPos := Pos('</blockquote>', Line);
  264.       if EndPos = 0 then
  265.         EndPos := Length(Line)
  266.       else
  267.         EndPos := EndPos - 1;
  268.       Value := Value + Copy(Line, 1, EndPos) + ' ';
  269.     until Pos('</blockquote>', Line) > 0;
  270.     HTMLDecode(Value);
  271.     Value := StringReplace(Value, '<br>', #13#10);
  272.     Value := StringReplace(Value, #13#10+' ', #13#10);
  273.     SetField(fieldComments, Value);
  274.   end;
  275.  
  276.   // Length
  277.   LineNr := FindLine('Runtime:', Page, 0);
  278.   if LineNr > -1 then
  279.   begin
  280.     Line := Page.GetString(LineNr + 1);
  281.     EndPos := pos(' min', Line);
  282.     if EndPos = 0 then
  283.       EndPos := pos('  /', Line);
  284.     if EndPos = 0 then
  285.       EndPos := Length(Line);
  286.     if Pos(':', Line) < EndPos then
  287.       BeginPos := Pos(':', Line) + 1
  288.     else
  289.       BeginPos := 1;
  290.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  291.     SetField(fieldLength, Value);
  292.   end;
  293.  
  294.   // Language
  295.   LineNr := FindLine('Language:', Page, 0);
  296.   if LineNr > -1 then
  297.   begin
  298.     Line := Page.GetString(LineNr + 1);
  299.     BeginPos := pos('/">', Line) + 3;
  300.     EndPos := pos('</a>', Line);
  301.     if EndPos = 0 then
  302.       EndPos := Length(Line);
  303.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  304.     SetField(fieldLanguages, Value);
  305.   end;
  306.  
  307.   DisplayResults;
  308. end;
  309.  
  310. function GetDescriptions(Address: string): string;
  311. var
  312.   Line, Value: string;
  313.   LineNr: Integer;
  314.   BeginPos, EndPos,Longest: Integer;
  315.   Page: TStringList;
  316. begin
  317.   Result := '';
  318.   Longest := 0;
  319.   Page := TStringList.Create;
  320.   Page.Text := GetPage(Address);
  321.   LineNr := FindLine('<p class="plotpar">', Page, 0);
  322.   while LineNr > -1 do
  323.   begin
  324.     Value := '';
  325.     repeat
  326.       Line := Page.GetString(LineNr);
  327.       BeginPos := pos('"plotpar">', Line);
  328.       if BeginPos > 0 then
  329.         BeginPos := BeginPos + 10
  330.       else
  331.         BeginPos := 1;
  332.       EndPos := pos('</p>', Line);
  333.       if EndPos < 1 then
  334.         EndPos := Length(Line) + 1;
  335.       if Value <> '' then
  336.         Value := Value + ' ';
  337.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  338.       LineNr := LineNr + 1;
  339.     until (pos('</p>', Line) > 0) or (LineNr = Page.Count);
  340.     HTMLDecode(Value);
  341.     PickListAdd(Value);
  342.  
  343.     if Length(Value) > Longest then
  344.     begin
  345.       Result := Value;
  346.       Longest := Length(Value);
  347.     end;
  348.  
  349.     LineNr := FindLine('<p class="plotpar">', Page, LineNr);
  350.   end;
  351.   Page.Free;
  352. end;
  353.  
  354. procedure AddMoviesTitles(Page: TStringList; Tag: string);
  355. var
  356.   Line: string;
  357.   LineNr: Integer;
  358.   MovieTitle, MovieAddress: string;
  359.   StartPos: Integer;
  360. begin
  361.   LineNr := FindLine(tag, Page, 0);
  362.   if LineNr > -1 then
  363.   begin
  364.     Line := Page.GetString(LineNr);
  365.     HTMLRemoveTags(Line);
  366.     PickTreeAdd(Trim(Line), '');
  367.     LineNr := LineNr + 5;
  368.     Line := Page.GetString(LineNr);
  369.     repeat
  370.       StartPos := pos('href="', Line) + 5;
  371.       Delete(Line, 1, StartPos);
  372.       MovieAddress := Copy(Line, 1, pos('">', Line) - 1);
  373.       StartPos := Pos('">', Line) + 2;
  374.       MovieTitle := Copy(Line, StartPos, Pos('</a>', Line) - StartPos);
  375.       HTMLDecode(Movietitle);
  376.       PickTreeAdd(MovieTitle, 'http://us.imdb.com' + MovieAddress);
  377.       LineNr := LineNr + 2;
  378.       Line := Page.GetString(LineNr);
  379.     until Pos('</table>', Line) > 0;
  380.   end;
  381. end;
  382.  
  383. begin
  384.   if CheckVersion(3,4,0) then
  385.   begin
  386.     MovieName := GetField(fieldOriginalTitle);
  387.     if MovieName = '' then
  388.       MovieName := GetField(fieldTranslatedTitle);
  389.     if Input('IMDb Import', 'Enter the title of the movie:', MovieName) then
  390.     begin
  391.       AnalyzePage('http://us.imdb.com/Tsearch?title='+UrlEncode(MovieName));
  392.     end;
  393.   end
  394.   else
  395.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
  396. end.
  397.  
  398.